﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class EmployInCompanyController : BaseController<emplyinfo>
    {
        //公司内部员工花名册
        // GET: /EmployInCompany/

        //public ActionResult Index()
        //{
        //    return View();
        //}
        public override BLL.BaseBll<emplyinfo> bll
        {
            get
            {
                return new EmplyInfoBll();
            }

        }
        //加载数据列表 查询方法
        public override ActionResult Get(int page, int rows, emplyinfo model)
        {
            int count;
            //emplyID  emplyNumber emplyName emplySex firstLevelDpt secondLevelDpt  employJob
            //entryTime workYears workYears numOfSign theLastSignDate   contractLength  contractExpireDate 
            //emplyCardID  emplyBornDate emplyAge emplyJiGuan huJiPlace  emplyAddress  emplyNation graduatUniversity  emplyMajor 
            //emplyDegree emplyMarryState emplyPolityFace emplyPhone emergencyContact 
            //contactPhone  workForm probationTime  actuallyBecomeDate
            //isBecome mark
            var list = bll.Search(model, x => true, true, x => x.emplyID, rows, page, out count).Select(x => new
            {
                x.emplyID,
                x.emplyNumber,
                x.emplyName,
                emplySex = x.emplySex == 1 ? "女" : "男",
                isManager = x.isManager == 1 ? "是" : "否",
                x.firstLevelDpt,
                x.secondLevelDpt,
                x.employJob,
                entryTime = x.entryTime,
                x.workYears,
                x.workYMD,
                x.numOfSign,
                x.theLastSignDate,
                x.contractLength,
                x.contractExpireDate,
                x.emplyCardID,
                x.emplyBornDate,
                x.emplyAge,
                x.emplyJiGuan,
                x.huJiPlace,
                x.emplyAddress,
                emplyNation = x.emplyNation == 1 ? "汉族" : x.emplyNation == 2 ? "回族" : x.emplyNation == 3 ? "满族" : "其他",
                x.graduatUniversity,
                x.emplyMajor,
                emplyDegree = x.emplyDegree == 1 ? "博士" : x.emplyDegree == 2 ? "硕士" : x.emplyDegree == 3 ? "本科" : x.emplyDegree == 4 ? "大专" : x.emplyDegree == 5 ? "中专" : x.emplyDegree == 6 ? "高中" : x.emplyDegree == 7 ? "初中" : "其他",
                emplyMarryState = x.emplyMarryState == 1 ? "已婚" : "未婚",
                emplyPolityFace = x.emplyPolityFace == 1 ? "群众" : x.emplyPolityFace == 2 ? "党员" : "其他",
                x.emplyPhone,
                x.emergencyContact,
                x.contactPhone,
                workForm = x.workForm == 1 ? "全日制" : x.workForm == 0 ? "兼职" : "其他",
                x.probationTime,
                x.actuallyBecomeDate,
                isBecome = x.isBecome == 1 ? "是" : "否",
                x.mark,
                x.password,
            }).ToList();
            return Json(new { total = count, rows = list });
        }

        public class workTime
        {
            public int? emplyAge { get; set; }
            public int? workYears { get; set; }
        }


        //新增用户  
        [HttpPost]
        public override ActionResult Save(emplyinfo model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.emplyName != "" && model.employJob != "")
            {
                var count = bll.Search(x => x.emplyNumber == model.emplyNumber).Count();
                //判断该员工工号是否存在   entryTime
                // https://www.cnblogs.com/liyunhua/p/4495606.html js实现身份证号码验证
                if (count == 0)
                {
                    string sqlWorkYears = "SELECT TIMESTAMPDIFF(YEAR,'" + model.entryTime + "',NOW()) as workYears";
                    model.workYears = bll.SearchBySQL<workTime>(sqlWorkYears).FirstOrDefault().workYears;
                    string sqlAge = "SELECT TIMESTAMPDIFF(YEAR,'" + model.emplyBornDate + "',NOW()) as emplyAge";
                    model.emplyAge = bll.SearchBySQL<workTime>(sqlAge).FirstOrDefault().emplyAge;
                    //如果 最近一次签订合同日期不为空   合同年限不为空  则 合同到期日期不为空
                    if (!String.IsNullOrEmpty(model.theLastSignDate.ToString()) && model.contractLength != 0)
                    {
                        //   
                        model.contractExpireDate = Convert.ToDateTime(model.theLastSignDate).AddYears(Convert.ToInt32(model.contractLength)).AddDays(-1);
                    }
                    else
                    {
                        model.contractExpireDate = null;
                    }
                    model.state = 1;
                    model.isManager = 0;
                    model.createtime = DateTime.Now;
                    int num = 0;
                    for (int i = 0; i < 5; i++)
                    {
                        Random r = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")) + i);
                        num = r.Next(10000000, 99999999);//随机生成一个5位整数

                        string tempStr = string.Empty;
                        for (int j = 0; j < 4; j++)
                        {
                            r = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")) + i + j);
                            int x = r.Next(65, 90);//65-90代表A-Z的ASCII值 
                            char a = (char)x;
                            tempStr += a.ToString();
                        }
                        //Console.WriteLine("num" + i.ToString() + "的值是：" + num);
                        //Console.WriteLine("tempStr" + i.ToString() + "的值是：" + tempStr);
                    }
                    model.password = num.ToString();
                    if (bll.Add(model) > 0)
                    {
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                }
                else
                {
                    jieguo = 0;
                    msg = "该账号已经存在，请重新填写";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }


        public ActionResult GetOnlyOne(long id)
        {
            //https://www.cnblogs.com/menyiin/p/5363707.html  时间控件
            return Json(bll.Search(x => x.emplyID == id).Select(x => new
            {
                x.emplyID,
                x.emplyNumber,
                x.emplyName,
                emplySex = x.emplySex == 1 ? "女" : "男",
                x.firstLevelDpt,
                x.secondLevelDpt,
                x.employJob,
                entryTime = x.entryTime, //"2017-09-26",
                x.workYears,
                x.workYMD,
                x.numOfSign,
                x.theLastSignDate,
                x.contractLength,
                x.contractExpireDate,
                x.emplyCardID,
                x.emplyBornDate,
                x.emplyAge,
                x.emplyJiGuan,
                x.huJiPlace,
                x.emplyAddress,
                x.emplyNation,
                x.graduatUniversity,
                x.emplyMajor,
                x.emplyDegree,
                emplyMarryState = x.emplyMarryState == 1 ? "已婚" : "未婚",
                emplyPolityFace = x.emplyPolityFace == 1 ? "群众" : x.emplyPolityFace == 2 ? "党员" : "其他",
                x.emplyPhone,
                x.emergencyContact,
                x.contactPhone,
                workForm = x.workForm == 1 ? "全日制" : x.workForm == 0 ? "兼职" : "其他",
                x.probationTime,
                x.actuallyBecomeDate,
                x.createtime,
                isBecome = x.isBecome == 1 ? "是" : "否",
                x.mark,
            }).ToList()[0], JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// 修改 密码   
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public override ActionResult Modify(emplyinfo model)
        {
            string msg = "修改失败";
            string state = "false";
            if (model.emplyID != 0 && model.emplyName != null)
            {
                // https://www.cnblogs.com/liyunhua/p/4495606.html js实现身份证号码验证
                string sqlWorkYears = "SELECT TIMESTAMPDIFF(YEAR,'" + model.entryTime + "',NOW()) as workYears";
                model.workYears = bll.SearchBySQL<workTime>(sqlWorkYears).FirstOrDefault().workYears;
                string sqlAge = "SELECT TIMESTAMPDIFF(YEAR,'" + model.emplyBornDate + "',NOW()) as emplyAge";
                model.emplyAge = bll.SearchBySQL<workTime>(sqlAge).FirstOrDefault().emplyAge;
                //如果 最近一次签订合同日期不为空   合同年限不为空  则 合同到期日期不为空
                if (!String.IsNullOrEmpty(model.theLastSignDate.ToString()) && model.contractLength != 0)
                {
                    model.contractExpireDate = Convert.ToDateTime(model.theLastSignDate).AddYears(Convert.ToInt32(model.contractLength)).AddDays(-1);
                }
                else
                {
                    model.contractExpireDate = null;
                }
                model.state = 1;
                model.isManager = 0;
                int num = 0;
                for (int i = 0; i < 5; i++)
                {
                    Random r = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")) + i);
                     num = r.Next(10000000, 99999999);//随机生成一个5位整数

                   string tempStr = string.Empty;
                    for (int j = 0; j < 4; j++)
                    {
                        r = new Random(int.Parse(DateTime.Now.ToString("HHmmssfff")) + i + j);
                        int x = r.Next(65, 90);//65-90代表A-Z的ASCII值 
                        char a = (char)x;
                        tempStr += a.ToString();
                    }                  
                    //Console.WriteLine("num" + i.ToString() + "的值是：" + num);
                    //Console.WriteLine("tempStr" + i.ToString() + "的值是：" + tempStr);
                }
                model.password = num.ToString();
                if (bll.Modify(model) > 0)
                {
                    msg = "修改成功";
                    state = "true";
                }
            }
            else
            {
                msg = "请填写完整";
                return Json(new { state = state, msg = msg });
            }

            return Json(new { state = state, msg = msg });
        }

        public ActionResult Delete(string id)
        {
            string msg = "删除失败";
            string state = "false";
            long eid = Convert.ToInt32(id);
            if (bll.Delete(x => x.emplyID == eid) > 0)
            {
                msg = "删除成功";
                state = "true";
            }
            return Json(new { state = state, msg = msg });
        }
    }
}
